package cn.bugDance.domain.activity.service;


import cn.bugDance.domain.activity.model.aggregate.ActivitySkuOrderAggregate;
import cn.bugDance.domain.activity.model.entity.*;
import cn.bugDance.domain.activity.model.valobj.ActivitySkuStockSubtractionJobData;
import cn.bugDance.domain.activity.model.valobj.TradeTypeVO;
import cn.bugDance.domain.activity.repository.IActivityRepository;
import cn.bugDance.domain.activity.service.policy.ITradePolicy;
import cn.bugDance.domain.activity.service.rule.factory.RuleChainFactory;
import cn.bugDance.types.enums.ResponseCode;
import cn.bugDance.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;


@Slf4j
public abstract class AbstractRaffleActivityService implements IRaffleActivityService{

    @Resource
    private IActivityRepository activityRepository;

    @Autowired
    @Qualifier("activityRuleChainFactory")
    private RuleChainFactory ruleChainFactory;

    Map<String, ITradePolicy> tradePolicyGroup;

    public AbstractRaffleActivityService(Map<String, ITradePolicy> tradePolicyGroup) {
        this.tradePolicyGroup = tradePolicyGroup;
    }

    @Override
    public ActivitySkuOrderEntity createActivitySkuOrder(ActivityShopCartEntity activityShopCartEntity) {
        Long sku = activityShopCartEntity.getSku();
        String userId = activityShopCartEntity.getUserId();
        String bizId = activityShopCartEntity.getBizId();
        TradeTypeVO tradeType = activityShopCartEntity.getTradeType();

        if(StringUtils.isBlank(userId) || StringUtils.isBlank(bizId) || null == sku){
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        ActivitySkuOrderEntity activitySkuOrderEntity = activityRepository.queryUnPaidActivitySkuOrder(sku, userId, tradeType.getCode());
        if(activitySkuOrderEntity != null){
            return activitySkuOrderEntity;
        }

        log.info("【活动服务层-RaffleActivityService】: 创建活动sku订单, sku: {}, userId: {}", sku, userId);

        ActivitySkuEntity activitySkuEntity = activityRepository.queryActivitySkuBySku(sku);
        ActivityEntity activityEntity = activityRepository.queryActivityByActivityId(activitySkuEntity.getActivityId());
        ActivityCountEntity activityCountEntity = activityRepository.queryActivityCountByCountId(activitySkuEntity.getActivityCountId());

        ruleChainFactory.openRuleChain(activityEntity, activityCountEntity, activitySkuEntity);

        ActivitySkuOrderAggregate activitySkuOrderAggregate = buildActivitySkuOrderAggregate(activitySkuEntity, activityEntity, activityCountEntity, userId, bizId);

        ITradePolicy tradePolicy = tradePolicyGroup.get(tradeType.getCode());
        tradePolicy.trade(activitySkuOrderAggregate);

        return activitySkuOrderAggregate.getActivitySkuOrderEntity();
    }

    protected abstract ActivitySkuOrderAggregate buildActivitySkuOrderAggregate(ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity, String userId, String bizId);

    protected abstract void doSaveOrder(ActivitySkuOrderAggregate activitySkuOrderAggregate);
}
